<?php

namespace backend\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\User as UserModel;

/**
 * User represents the model behind the search form about `\backend\models\User`.
 */
class User extends UserModel
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'reg_time', 'last_login_time', 'age', 'sex', 'province', 'city', 'area', 'birth_day', 'status', 'token_expire_time', 'is_tanmu', 'is_push', 'level', 'broke_level', 'comment_level', 'star_sign', 'experience', 'lebe', 'login_type', 'is_first', 'is_invited', 'person_level', 'is_reward', 'fans_level', 'is_sub'], 'integer'],
            [['user_name', 'password', 'phone', 'email', 'address', 'header', 'user_cat', 'user_token', 'signature', 'tags', 'invitation_code', 'comment_vote', 'login_id'], 'safe'],
            [['money', 'amount', 'total_amount'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = UserModel::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'reg_time' => $this->reg_time,
            'last_login_time' => $this->last_login_time,
            'money' => $this->money,
            'age' => $this->age,
            'sex' => $this->sex,
            'province' => $this->province,
            'city' => $this->city,
            'area' => $this->area,
            'birth_day' => $this->birth_day,
            'status' => $this->status,
            'token_expire_time' => $this->token_expire_time,
            'is_tanmu' => $this->is_tanmu,
            'is_push' => $this->is_push,
            'level' => $this->level,
            'broke_level' => $this->broke_level,
            'comment_level' => $this->comment_level,
            'star_sign' => $this->star_sign,
            'experience' => $this->experience,
            'lebe' => $this->lebe,
            'login_type' => $this->login_type,
            'is_first' => $this->is_first,
            'is_invited' => $this->is_invited,
            'person_level' => $this->person_level,
            'is_reward' => $this->is_reward,
            'fans_level' => $this->fans_level,
            'is_sub' => $this->is_sub,
            'amount' => $this->amount,
            'total_amount' => $this->total_amount,
        ]);

        $query->andFilterWhere(['like', 'user_name', $this->user_name])
            ->andFilterWhere(['like', 'password', $this->password])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'header', $this->header])
            ->andFilterWhere(['like', 'user_cat', $this->user_cat])
            ->andFilterWhere(['like', 'user_token', $this->user_token])
            ->andFilterWhere(['like', 'signature', $this->signature])
            ->andFilterWhere(['like', 'tags', $this->tags])
            ->andFilterWhere(['like', 'invitation_code', $this->invitation_code])
            ->andFilterWhere(['like', 'comment_vote', $this->comment_vote])
            ->andFilterWhere(['like', 'login_id', $this->login_id]);

        return $dataProvider;
    }
}
